package com.cl.juc;

import java.util.concurrent.CountDownLatch;

public class TestCountDownLatch {

    /*
    * 闭锁
    * 构造方法初始化n把锁
    * 调用 countDownLatch.await(); 会被阻塞,进入阻塞队列, 直到调用 countDownLatch.countDown(); 使countDownLatch中
    * 初始化的闭锁中的count减为0, 被await的线程就会被唤醒
    *
    * 每调用一次 countDownLatch.countDown(); ,闭锁中的count就会-1
    * */
    private static final CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < countDownLatch.getCount(); i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() +" countDown...");
                countDownLatch.countDown();
            },"thread-"+i).start();
        }


        countDownLatch.await();
        System.out.println("主线程结束");
    }


}
